package com.other.generater;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;

/**
 * 类功能描述:
 *
 * @author baigt
 * @version V1.0
 * @since 2019-06-17 16:33
 */
public class Test2 {
    public static void main(String[] args) throws Exception {
        String remotepwd="95q5pada " + Code.main2("UVGYQCGFVGLTKBQ7");
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession("baigt", "10.10.250.249", 18822);
        session.setPassword(remotepwd);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.connect();
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        channelExec.setCommand("ls");
        channelExec.setInputStream(null);
        channelExec.setErrStream(System.err);
        channelExec.connect();
        InputStream in = channelExec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
        String buf = null;
        StringBuffer sb = new StringBuffer();
        while ((buf = reader.readLine()) != null) {
            sb.append(buf);
            System.out.println(buf);// 打印控制台输出
        }
        reader.close();
        channelExec.disconnect();
        if (null != session) {
            session.disconnect();
        }

    }
    public static void main3(String[] args) throws Exception {
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession("prescription-service", "192.168.1.76", 22);
        session.setPassword("prescription-service");
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.connect();
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        channelExec.setCommand("ls");
        channelExec.setInputStream(null);
        channelExec.setErrStream(System.err);
        channelExec.connect();
        InputStream in = channelExec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
        String buf = null;
        StringBuffer sb = new StringBuffer();
        while ((buf = reader.readLine()) != null) {
            sb.append(buf);
            System.out.println(buf);// 打印控制台输出
        }
        reader.close();
        channelExec.disconnect();
        if (null != session) {
            session.disconnect();
        }

    }
    public static void main2(String[] args)throws Exception {
        try {
//        Connection conn = new Connection("10.10.250.249",18822);
        Connection conn = new Connection("192.168.1.76",22);
        conn.connect();
//        boolean isAuthenticated = conn.authenticateWithPassword("baigt", "95q5pada " + Code.main2("UVGYQCGFVGLTKBQ7"));
        boolean isAuthenticated = conn.authenticateWithPassword("prescription-service", "prescription-service");
        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");
        Session sess = conn.openSession();
        sess.execCommand("ls");
//        InputStream stdout = new StreamGobbler(sess.getStdout());
        InputStream stdout = sess.getStdout();
        BufferedReader br = new BufferedReader(
                new InputStreamReader(stdout));
            if (br.ready()) {
                System.out.println("ready");
            } else {
                System.out.println("not ready");
            }
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        sess.close();
        conn.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(2);
    }

}
}
